import SwiftUI
import SPGlassKit

public struct GlassAgentPanelExample: GlassExample {
    public static let componentID = "glass-agent-panel"

    /// Fictitious tasks: nothing is executed, the example only edits these values.
    @State private var tasks = [
        GlassAgentTask(id: "t1", title: "Prepare the sample weekly digest", status: .running, steps: [
            GlassAgentStep(id: "s1", title: "Collect sample notes", status: .succeeded, detail: "12 notes found"),
            GlassAgentStep(id: "s2", title: "Group by topic", status: .running, detail: "Topic 3 of 5"),
            GlassAgentStep(id: "s3", title: "Write the digest", status: .pending),
        ]),
        GlassAgentTask(id: "t2", title: "Rename sample files with a deliberately long task title that wraps over two lines", status: .failed, steps: [
            GlassAgentStep(id: "s1", title: "List files", status: .succeeded),
            GlassAgentStep(id: "s2", title: "Apply new names", status: .failed, detail: "4 of 9 renamed", errorMessage: "Sample failure: a file with this name already exists."),
        ]),
        GlassAgentTask(id: "t3", title: "Translate sample page", status: .succeeded, steps: [
            GlassAgentStep(id: "s1", title: "Detect language", status: .succeeded),
            GlassAgentStep(id: "s2", title: "Translate", status: .succeeded),
        ]),
        GlassAgentTask(id: "t4", title: "Queued sample export", status: .pending),
        GlassAgentTask(id: "t5", title: "Sample clean-up", status: .cancelled, steps: [
            GlassAgentStep(id: "s1", title: "Scan", status: .cancelled),
        ], errorMessage: nil),
    ]

    public init() {}

    public var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 12) {
                Text("Sample data · simulated tasks, no agent is running")
                    .font(.caption)
                    .foregroundStyle(.secondary)
                GlassAgentPanel(tasks: tasks) { task in
                    cancel(task.id)
                }
                GlassButton("Advance the running task", systemImage: "forward.frame") { advance() }
                Divider()
                Text("No task")
                    .font(.headline)
                GlassAgentPanel(tasks: [])
            }
            .padding()
        }
    }

    private func cancel(_ id: String) {
        guard let index = tasks.firstIndex(where: { $0.id == id }) else { return }
        tasks[index].status = .cancelled
        for step in tasks[index].steps.indices where !tasks[index].steps[step].status.isTerminal {
            tasks[index].steps[step].status = .cancelled
        }
    }

    /// Completes the running step of the first running task and starts the next one.
    private func advance() {
        guard let index = tasks.firstIndex(where: { $0.status == .running }) else { return }
        if let running = tasks[index].steps.firstIndex(where: { $0.status == .running }) {
            tasks[index].steps[running].status = .succeeded
        }
        if let next = tasks[index].steps.firstIndex(where: { $0.status == .pending }) {
            tasks[index].steps[next].status = .running
        } else {
            tasks[index].status = .succeeded
        }
    }
}

#Preview("GlassAgentPanel") { GlassAgentPanelExample().glassTheme(.obsidian) }
